package com.java2007.hotel.controller.backend;

import com.alibaba.druid.util.StringUtils;
import com.java2007.hotel.constant.ServletConstant;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.Food;
import com.java2007.hotel.pojo.FoodType;
import com.java2007.hotel.service.FoodService;
import com.java2007.hotel.service.FoodTypeService;
import com.java2007.hotel.utils.FileUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author ghy
 * @version 1.0
 * @date 2021-01-22
 */
@WebServlet("/food")
@MultipartConfig
public class FoodController extends BaseServlet {

    private FoodService foodService = (FoodService) BeanFactory.getBean("com.java2007.hotel.service.impl.FoodServiceImpl");

    private FoodTypeService foodTypeService = (FoodTypeService) BeanFactory.getBean("com.java2007.hotel.service.impl.FoodTypeServiceImpl");


    /**
     * 回显菜品数据
     *  1. 查询菜类别数据
     *  2. 回显选中的菜类别，图片
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String findById(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //菜类别列表数据
        List<FoodType> types = foodTypeService.findAll();
        request.setAttribute("types", types);

        String idStr = request.getParameter("id");
        int id = Integer.parseInt(idStr);
        //菜品数据
        Food food = foodService.findById(id);
        request.setAttribute("food", food);
        //图片回显的地址: http://localhost:8080/项目虚拟路径/files/uuid.jpg

        //图片回显的地址: http://localhost:8080/项目虚拟路径 + food.getImage()

        return ServletConstant.PRE_FORWARD + "/backend/detail/food/food-update.jsp";
    }

    /**
     * 到保存页面
     *  获取 菜类别列表数据
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String saveui(HttpServletRequest request, HttpServletResponse response) throws Exception{
        List<FoodType> types = foodTypeService.findAll();
        request.setAttribute("types", types);
        return ServletConstant.PRE_FORWARD + "/backend/detail/food/food-save.jsp";
    }

    /**
     * 保存
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String save(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //文件上传
        /*Part part = request.getPart("image");
        System.out.println(part);
        String filename = part.getSubmittedFileName();
        String suffixName = filename.substring(filename.lastIndexOf(".")); //.jpg

        //获取files目录在服务器中的真实路径【某个具体路径下的目录】
        String imagePath = request.getSession().getServletContext().getRealPath("/files/");
        System.out.println("-->" + imagePath);

        String fileName = UUIDUtil.getFileName(suffixName);   //uuid.jpg
        File file = new File(imagePath, fileName);
        if(!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        part.write(file.getPath());
*/
        String fileName = FileUtil.upload(request);

        if(StringUtils.isEmpty(fileName)) {
            //文件上传错误
            return "文件上传错误";
        }

        //封装参数，执行保存业务
        Map<String, String[]> parameterMap = request.getParameterMap();
        Food food = new Food();
        BeanUtils.populate(food, parameterMap);
        //把图片存为一个服务上的映射资源的路径
        food.setImage("/files/" + fileName);   //   /files/uuid.jpg  --> http://localhost:8080/项目虚拟路径/files/uuid.jpg

        foodService.save(food);

        return  "增加成功";
    }

    /**
     * 保存
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String fileName = FileUtil.upload(request);

        if(StringUtils.isEmpty(fileName)) {
            //文件上传错误
            return "文件上传错误";
        }

        //封装参数，执行保存业务
        Map<String, String[]> parameterMap = request.getParameterMap();
        Food food = new Food();
        BeanUtils.populate(food, parameterMap);
        //把图片存为一个服务上的映射资源的路径
        food.setImage("/files/" + fileName);   //   /files/uuid.jpg  --> http://localhost:8080/项目虚拟路径/files/uuid.jpg

        foodService.save(food);

        return  "增加成功";
    }


}
